package com.starmark.core.framework.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starmark.core.framework.exception.UniqueException;
import com.starmark.core.framework.util.Query;
import com.starmark.core.framework.vo.PageQueryVo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;


/**
 * 业务逻辑层基类
 *
 * @author starmark
 * @version 2018年5月20日 下午3:19:19
 */
@Slf4j
public abstract class AbstractUniqueBaseService<Mapper extends SuperMapper<T>, T extends BaseModel> extends AbstractBaseService<Mapper,T> implements
        IUniqueBaseService<T> {


    @Override
    public boolean save(T entity){
        if(entity instanceof IUniqueModel ) {
            if (this.isUniqueRecord((IUniqueModel)entity)) {
                throw new UniqueException();
            }
        }
        return super.save(entity);
    }
    /**
     * 是否唯一的编码
     * @param model 业务对象
     * @return 是否唯一
     */
    @Override
    public boolean isUniqueRecord(IUniqueModel model){
        QueryWrapper<T> eWrapper = new QueryWrapper<>();
        eWrapper.eq(this.getDbFieldCode(),model.uniqueCode());
        T record=this.getOne(eWrapper,false);
        return record!=null;
    }
}
